<?php
class class_ABMProveedores
{
	private $_db; 
	function __construct(){
		$this->config();
	}
	
	function method_getLista($params, $error){
		$this->_db->setQuery("SELECT razones_sociales.*, proveedores.persona FROM razones_sociales LEFT JOIN proveedores ON razones_sociales.cuit = proveedores.cuit ORDER BY razones_sociales.descripcion");
		$this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$result = array();
		$regs = $this->_db->loadResult();
		$row = array();
    	FOREACH ($regs as $reg){
			array_push($row, $reg->cod_razon_social);
			array_push($row, $reg->cuit);
			array_push($row, $reg->descripcion);
			array_push($row, $reg->telefono1);
			array_push($row, $reg->telefono2);
			array_push($row, $reg->email);
			array_push($row, $reg->persona);
			array_push($result, $row);
			$row = array();
		}
		return $result;
    }
    
	function method_btnBorrar($params, $error){
        if (count($params) != 1){ $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
        $this->_db->setQuery("DELETE FROM razones_sociales WHERE cod_razon_social = '" . $params[0] . "'");
        $this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    
	function method_btnModificar($params, $error){
        if (count($params) != 1){ $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
		$this->_db->setQuery("SELECT razones_sociales.*, proveedores.persona FROM razones_sociales LEFT JOIN proveedores ON razones_sociales.cuit = proveedores.cuit WHERE cod_razon_social = '" . $params[0] . "' LIMIT 1");
		$this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$reg = $this->_db->loadResult();
		return $reg[0];
		
    }
    
	function method_opAlta($params, $error) {
        if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
        
		mysql_query("begin");
			$this->_db->setQuery("SELECT cuit FROM proveedores WHERE cuit =  '" . $params[0]->cuit . "' LIMIT 1");
			if(count($this->_db->loadResult()) > 0 ){
				$query1 = 1;
			}
			else{
				$this->_db->setQuery("INSERT INTO proveedores (cuit, persona) VALUES ('" . $params[0]->cuit . "', '" . $params[0]->persona . "')");
			}
			$query1 = $this->_db->alter();
			$this->_db->setQuery("INSERT INTO razones_sociales (cod_razon_social, descripcion, cuit, telefono1, telefono2, email) VALUES ('" . $params[0]->cod_razon_social. "', '" . $params[0]->descripcion. "', '" . $params[0]->cuit. "', '" . $params[0]->telefono1. "', '" . $params[0]->telefono2. "', '" . $params[0]->email. "')");
			$query2 = $this->_db->alter();
		if($query1 && $query2){
			mysql_query("commit");
			return true;
		}
		else{
			mysql_query("rollback");
			$error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 6) . ": " . mysql_error())); return $error;
		}
    }
    
	function method_opModificacion($params, $error){
		if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
	    $this->_db->setQuery("UPDATE proveedores SET persona = '" . $params[0]->persona. "' WHERE cuit = '" . $params[0]->cuit . "' LIMIT 1");
	    $this->_db->alter();
	    $this->_db->setQuery("UPDATE razones_sociales SET descripcion = '" . $params[0]->descripcion. "', telefono1 = '" . $params[0]->telefono1. "', telefono2 = '" . $params[0]->telefono2. "', email = '" . $params[0]->email. "' WHERE cuit = '" . $params[0]->cuit . "' LIMIT 1");
        $this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    
	private function config(){
		require_once("conexion.php");
		require_once("DataBase.php");
		$this->_db = new DataBase($SERVIDOR,$USUARIO,$PASSWORD,$BASE);
		mysql_query("SET NAMES utf8");
	}
}
?>